import scrapy


class ProducerSpider(scrapy.Spider):
    name = "producer"

    def start_requests(self):
        for i in range(1, 500):
            data = {
                "worksTypes": "0",
                "bookType": "0",
                "subWorksTypes": "0",
                "totalWord": "0",
                "serialStatus": "1",
                "vip": "9",
                "totalWold": "0",
                "pageNum": str(i),
                "pageSize": "20",
                "categoryId": "0",
                "categoryPid": "0",
                "order": "weekOrder",
                "naodongFilter": "0"
            }
            yield scrapy.FormRequest(url='https://www.zongheng.com/api2/catefine/storeSearch', formdata=data,
                                     callback=self.parse1)



    def parse1(self, response):
        for item in response.json()['result']['bookList']:
            book_id = item['bookId']
            # print(book_id)
            data = {
                "bookId": str(book_id),
            }
            yield scrapy.FormRequest(url='https://bookapi.zongheng.com/api/chapter/getChapterList',
                                     callback=self.parse2, formdata=data)


    def parse2(self, response):
        result = response.json()
        # print(result['result']['chapterList'])
        for juan in result['result']['chapterList']:
            # print(juan['chapterViewList'])
            for item in juan['chapterViewList']:
                chapter_id = item['chapterId']
                book_id = item['bookId']
                chapter_name = item['chapterName']
                chapter_detail_url = f'https://read.zongheng.com/chapter/{book_id}/{chapter_id}.html'
                # print(chapter_name)
                yield {
                    'url': chapter_detail_url,
                    'meta': {
                        'book_id': book_id,
                        'chapter_id': chapter_id,
                        'chapter_name': chapter_name
                    }
                }

